package com.tgy;

import com.github.pagehelper.PageHelper;
import com.tgy.entity.User;
import com.tgy.mapper.UserMapper;
import com.tgy.utils.MyBatisUtil;
import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger;
import org.junit.Test;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/***
 * @ClassName: test
 * @Description:
 * @Auther: 送一一块二向箔
 * @Date: 18:29 2021/12/14
 * @version : V1.0
 */
public class test {
    @Test
    public void aa(){
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List<User> users = mapper.getUsers();
        System.out.println(users.toString());
        sqlSession.close();
    }
    static Logger logger = Logger.getLogger(test.class);
    @Test
    public void testLog4j(){

        logger.info("info:进入了testLog4j方法");
        logger.debug("debug:进入了testLog4j方法");
        logger.error("error:进入了testLog4j方法");
    }

    @Test
    public void t4(){
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        Map<String,Object> map = new HashMap<>();
        map.put("currentPage",1);
        map.put("pageSize",2);
        logger.info("开始分页查询{}");
        List<User> users =  userMapper.getUserByLimit(map);
        for (User user : users) {
            System.out.println(user);
        }
        sqlSession.close();
    }
    @Test
    public void t5(){
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        PageHelper.startPage(1,2);
        List<User> users =  userMapper.getUsers();
        for (User user : users) {
            System.out.println(user);
        }
        sqlSession.close();
    }

    @Test
    public void t6(){
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        PageHelper.startPage(1,2);
        List<User> users =  userMapper.getUsers();
        for (User user : users) {
            System.out.println(user);
        }
        sqlSession.close();
    }

    /**
     * @Param 测试缓存
     * @Description:
     * @auther: TangGuoyi
     * @date: 11:45 2021/12/16
     * @return:
     */
    @Test
    public void t7() {
        SqlSession session = MyBatisUtil.getSqlSession();
        UserMapper mapper = session.getMapper(UserMapper.class);

        User user = mapper.selectUserById(1);
        System.out.println(user);
        session.clearCache();//手动清理缓存
        System.out.println("===================");
        User user2 = mapper.selectUserById(1);
        System.out.println(user2);
        System.out.println(user==user2);

        session.close();
    }
    /**
     * @Param 测试二级缓存
     * @Description:
     * @auther: TangGuoyi
     * @date: 11:58 2021/12/16
     * @return:
     */
    @Test
    public void t8() {
        SqlSession session = MyBatisUtil.getSqlSession();
        SqlSession session2 = MyBatisUtil.getSqlSession();

        UserMapper mapper = session.getMapper(UserMapper.class);
        UserMapper mapper2 = session2.getMapper(UserMapper.class);

        User user = mapper.selectUserById(1);
        System.out.println(user);
        session.close();

        User user2 = mapper2.selectUserById(1);
        System.out.println(user2);
        System.out.println(user==user2);

        session2.close();
    }

}
